﻿using AutoMapper;
using MediatR;
using Provenance.Domain.DeepProcess;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.DeepProcess;
using Provenance.Infra.Repositories.DeepProcess;
using Provenance.Read.Api.Application.Command.DeepProcess;

namespace Provenance.Read.Api.Application.CommandHandler.DeepProcess
{
    public class BackAuxiliaryMaterialCommandHandler : IRequestHandler<BackAuxiliaryMaterialCommand, ApiResult<AuxiliaryMaterial>>
    {
        private readonly IAuxiliaryMaterialRepository _auxiliaryMaterialRepository;
        private readonly ILogger<BackAuxiliaryMaterialCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackAuxiliaryMaterialCommandHandler(IAuxiliaryMaterialRepository auxiliaryMaterialRepository, ILogger<BackAuxiliaryMaterialCommandHandler> logger, IMapper mapper)
        {
            _auxiliaryMaterialRepository = auxiliaryMaterialRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<AuxiliaryMaterial>> Handle(BackAuxiliaryMaterialCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _auxiliaryMaterialRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId);
                return ApiResult<AuxiliaryMaterial>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return ApiResult<AuxiliaryMaterial>.Failure(ex.Message);
            }
        }
    }
}
